//
//  GCMSBXQViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/11/17.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMSBXQViewController.h"
#import "GCMSheBeiRequst.h"
#import "GCMSHEBEIDetailModel.h"
#import "GCMSBPlanTableViewCell.h"

@interface GCMSBXQViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>

@end

@implementation GCMSBXQViewController{

    NSMutableArray *_dataSource;
    NSMutableArray *_datasource;
    UIScrollView *_scrollV;
    UITableView *_tableV;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitle];
    [self createTableView];
    [self downLoadData];
    [self createScrollV];
    
    
}

//创建头上面的
-(void)createTitle{
    
    UISegmentedControl * segmented = [[UISegmentedControl alloc] initWithItems:@[@"详情信息", @"附加信息"]];
    segmented.frame = CGRectMake(0, 0, 150, 30);
    segmented.selectedSegmentIndex = 0; //设置默认选中项
    [segmented addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged]; //添加事件
    self.navigationItem.titleView = segmented;
    
}

- (void)segmentedAction:(id)sender {
    if ([sender isKindOfClass:[UISegmentedControl class]]) {
        UISegmentedControl * segment = sender;
        if (segment.selectedSegmentIndex == 0) {
            _scrollV.hidden = NO;
            _tableV.hidden  = YES;
        }else if (segment.selectedSegmentIndex == 1) {
            _scrollV.hidden = YES;
            _tableV.hidden = NO;
            [GCMSheBeiRequst postXQOtherDataWithSBId:self.sbid WithURL:SBOTHER_URL Success:^(id obj, NSError *err) {
                NSLog(@"%@",obj);
                _datasource = obj;
                [_tableV reloadData];
            }];
        }
    }
}

-(void)downLoadData{

    [GCMSheBeiRequst postDetailWithsbId:self.sbid Success:^(id obj, NSError *err) {
//        NSLog(@"....%@",obj);
        _dataSource = obj;
        [self reloadScrollViewDate];
    }];
}

-(void)reloadScrollViewDate{

    GCMSHEBEIDetailModel *delModel = [[GCMSHEBEIDetailModel alloc]init];
    delModel = _dataSource[0];
    NSMutableArray *arrInfo = [[NSMutableArray alloc]init];
    [arrInfo addObject:delModel.sbname];
    [arrInfo addObject:delModel.xinghao];
    [arrInfo addObject:delModel.guige];
    [arrInfo addObject:delModel.azdd];
    [arrInfo addObject:delModel.zzl];
    [arrInfo addObject:delModel.sysms];
    [arrInfo addObject:delModel.cd];
    [arrInfo addObject:delModel.zzs];
    [arrInfo addObject:delModel.ccbh];
    [arrInfo addObject:delModel.wxcc];
    [arrInfo addObject:delModel.jszl];
    [arrInfo addObject:delModel.ccdate];
    [arrInfo addObject:delModel.jcdate];
    [arrInfo addObject:delModel.azdate];
    [arrInfo addObject:delModel.ysdate];
    [arrInfo addObject:delModel.syyear];
    [arrInfo addObject:delModel.sbyz];
    [arrInfo addObject:delModel.eddy];
    [arrInfo addObject:delModel.eddl];
    [arrInfo addObject:delModel.edgl];
    [arrInfo addObject:delModel.edzs];
    [arrInfo addObject:delModel.gzjz];
    [arrInfo addObject:delModel.pl];
    [arrInfo addObject:delModel.ywy];
    [arrInfo addObject:delModel.inserttime];
    [arrInfo addObject:delModel.sbbianhao];
    [arrInfo addObject:delModel.sbmp];
    [arrInfo addObject:delModel.zt];
    [arrInfo addObject:delModel.czqk];
    [arrInfo addObject:delModel.inserttime];
    [arrInfo addObject:delModel.period];

    
    
    NSArray *arr  = @[@"设备名称：",@"设备型号：",@"设备规格：",@"安装地址：",@"总重量：",@"使用说明书：",@"产地：",@"制造商：",@"出厂编号：",@"外形尺寸：",@"技术资料",@"出厂日期：",@"进场日期：",@"安装日期",@"验收日期：",@"使用年限：",@"设备原值：",@"额定电压：",@"额定功率：",@"额定电流：",@"额定转速：",@"总做介质：",@"频率：",@"业务员：",@"插入时间：",@"编号：",@"设备铭牌：",@"状态：",@"操作情况：",@"操作日期：",@"周期："];
    for (int i = 0; i < arr.count; i ++) {
    
        UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/3, 64 +30 * i, SCREEN_WIDTH/3*2, 30)];
                if (i%2) {
                    lab2.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
                }
        lab2.text  = arrInfo[i];
        [_scrollV addSubview:lab2];
    }
   
}

-(void)createScrollV{

    //    创建可以滑动的视图
    _scrollV = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT )];
    _scrollV.delegate = self;
    _scrollV.alwaysBounceHorizontal = NO;
    _scrollV.alwaysBounceVertical = NO;
    _scrollV.showsHorizontalScrollIndicator = NO;
    _scrollV.showsVerticalScrollIndicator = NO;
    _scrollV.bounces = NO;
    _scrollV.contentOffset = CGPointMake(0, 0);
//    _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH, 33 *30 );
//    _scrollV.backgroundColor = [UIColor colorWithRed:.95 green:.95 blue:.95 alpha:1.0];
//    _scrollV.backgroundColor = [UIColor orangeColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:_scrollV];
    [self createUI];
}
-(void)createUI{

    NSArray *arr  = @[@"设备名称：",@"设备型号：",@"设备规格：",@"安装地址：",@"总重量：",@"使用说明书：",@"产地：",@"制造商：",@"出厂编号：",@"外形尺寸：",@"技术资料",@"出厂日期：",@"进场日期：",@"安装日期",@"验收日期：",@"使用年限：",@"设备原值：",@"额定电压：",@"而定功率：",@"额定电流：",@"额定转速：",@"总做介质：",@"频率：",@"业务员：",@"插入时间：",@"编号：",@"设备铭牌：",@"状态：",@"操作情况：",@"操作日期：",@"周期："];
    for (int i = 0; i < arr.count; i ++) {
        UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 64 +30 * i, SCREEN_WIDTH, 30)];
        if (i%2) {
            lab1.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1];
        }
        lab1.text = [NSString stringWithFormat:@"  %@",arr[i]];
        [_scrollV addSubview:lab1];

    }
    _scrollV.contentSize = CGSizeMake(SCREEN_WIDTH, (arr.count + 2) *30 );
}

-(void)createTableView{
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    _tableV.backgroundColor  = [UIColor redColor];
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableV];
    _tableV.hidden = YES;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GCMSBPlanTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMSBPlanTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    
        GCMOtherModel *planModel = _datasource[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setOtherModel:planModel];
        return cell;
        
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _datasource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    GCMOtherModel *planModel = _datasource[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"设备值:%@",planModel.value];
    CGSize size = CGSizeMake(SCREEN_WIDTH - 60,CGFLOAT_MAX);
    UIFont* theFont = [UIFont systemFontOfSize:17];
    //计算文字所占区域
    CGSize labelSize = [str boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : theFont} context:nil].size;
    return  80 + labelSize.height + 20 ;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
